import numpy as np
import csv
from datetime import datetime
from time import strftime
import matplotlib.pyplot as plt
import math
import random
from scipy import stats


def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    f = open(inputCSV)
    #ra = csv.DictReader(file(fn), dialect="excel")
    ra = csv.DictReader(f, dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = int(float(record[item]))
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print '===================================================='
    print "begin at " + getCurTime()

    filepath = 'C:/_DATA/migration_census_2009/'
    file = filepath + 'regressiondata.csv'
    
    #plt.figure().set_size_inches(10,15)
    fig = plt.figure(figsize=(10,15),facecolor='w') 
    
    #filepath = 'C:/_DATA/migration_census_2000/'
    #file = filepath + 'gravity_model_data.csv'
    tickfontsize = 10
    data = build_data_list(file)    #[dist,graphdist,grossout,grossin,popout,popin,vol]
    lndata = np.log(data)
    #plt.figure().set_size_inches(15,20)
    ax1 = plt.subplot(321)
    #ax1 = fig.add_subplot(321)
    plt.loglog(data[:,0]/1000, data[:,-1], marker = '.', linestyle = '', label = 'Original data', ms=4, color ='w')
    plt.xlabel('Distance (km)')
    #ax1.xaxis.set_label_coords(0.5, 0.95)
    ax1.xaxis.set_label_position('top')
    plt.xticks(fontsize = tickfontsize)
    plt.yticks(fontsize = tickfontsize)
    #ax1.set_xlabel('Distance (km)',ha='left', va='top')
    plt.ylabel('Flow volumn')

    ax2 = plt.subplot(322, sharey=ax1)
    plt.loglog(data[:,1], data[:,-1], marker = '.', linestyle = '', label = 'Original data', ms=4, color ='w')
    plt.setp(ax2.get_yticklabels(), visible=False)
    ax2.xaxis.set_label_position('top')
    plt.xticks(fontsize = tickfontsize)
    plt.yticks(fontsize = tickfontsize)
    plt.xlabel('Graph distance')

    ax3 = plt.subplot(323, sharey=ax1)
    plt.loglog(data[:,2], data[:,-1], marker = '.', linestyle = '', label = 'Original data', ms=4, color ='w')
    plt.xlabel('Gross flow of Origin')
    plt.ylabel('Flow volumn')
    ax3.xaxis.set_label_position('top')
    plt.xticks(fontsize = tickfontsize)
    plt.yticks(fontsize = tickfontsize)

    ax4 = plt.subplot(324, sharey=ax1)
    plt.loglog(data[:,3], data[:,-1], marker = '.', linestyle = '', label = 'Original data', ms=4, color ='w')
    plt.setp(ax4.get_yticklabels(), visible=False)
    plt.xlabel('Gross flow of Destination')
    ax4.xaxis.set_label_position('top')
    plt.xticks(fontsize = tickfontsize)
    plt.yticks(fontsize = tickfontsize)

    ax5 = plt.subplot(325, sharey=ax1)
    plt.loglog(data[:,4], data[:,-1], marker = '.', linestyle = '', label = 'Original data', ms=4, color ='w')
    plt.xlabel('Pop of Origin')
    plt.ylabel('Flow volumn')
    ax5.xaxis.set_label_position('top')
    plt.xticks(fontsize = tickfontsize)
    plt.yticks(fontsize = tickfontsize)

    ax6 = plt.subplot(326, sharey=ax1)
    plt.loglog(data[:,5], data[:,-1], marker = '.', linestyle = '', label = 'Original data', ms=4, color ='w')
    plt.setp(ax4.get_yticklabels(), visible=False)
    plt.xlabel('Pop of Destination')
    ax6.xaxis.set_label_position('top')
    plt.xticks(fontsize = tickfontsize)
    plt.yticks(fontsize = tickfontsize)
    
    plt.show()
    #plt.figure().set_size_inches(15,20)
    #plt.savefig(filepath + 'cnty_scatterplot_2000.png', transparent=True, dpi = 120)
    plt.clf()